package com.warrior.superdata.mysql.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.warrior.superdata.constants.ResultCode;
import com.warrior.superdata.mysql.entity.ApiTableRelation;
import com.warrior.superdata.exceptionhandler.ApplicationException;
import com.warrior.superdata.mysql.mapper.ApiTableRelationMapper;
import com.warrior.superdata.mysql.service.ApiTableRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description:
 * @ClassName: ApiTableRelationServiceImpl.java
 * @date: 2022/6/21 19:32
 * @author xujian
 */
@Service
@Slf4j
public class ApiTableRelationServiceImpl extends ServiceImpl<ApiTableRelationMapper, ApiTableRelation> implements ApiTableRelationService {

    @Override
    public void addList(List<ApiTableRelation> list) {
        if (!CollectionUtils.isEmpty(list)) {
            Set<String> associationTableNames = list.stream().map(ApiTableRelation::getAssociationTableName).collect(Collectors.toSet());
            if (list.size() != associationTableNames.size()) {
                throw new ApplicationException(ResultCode.FAIL_CODE, "关联表有重复");
            }
            this.saveBatch(list);
        }
    }

    @Override
    public void deleteByApiId(Integer apiId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("api_id", apiId);
        this.remove(queryWrapper);
    }

}
